package middleware

import (
	"github.com/zeromicro/go-zero/rest/httpx"
	"net/http"
	"path/filepath"
	"work-safety/api/internal/types"
)

type UserAgentMiddleware struct {
}

func NewUserAgentMiddleware() *UserAgentMiddleware {
	return &UserAgentMiddleware{}
}

func (m *UserAgentMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// TODO generate middleware implement function, delete after code implementation

		token := r.Header.Get("image")
		if token == "" {
			ronen := &types.Response{
				Message: "未登录",
				Coce:    0,
				Data:    nil,
			}
			httpx.OkJsonCtx(r.Context(), w, ronen)
			return
		}
		ext := filepath.Ext("image")
		if ext == "" {
			ronen := &types.Response{
				Message: "登录失败",
				Coce:    0,
				Data:    nil,
			}
			httpx.OkJsonCtx(r.Context(), w, ronen)
			return
		}
		r.ParseForm()
		// Passthrough to next handler if need
		next(w, r)
	}
}
